package com.superman.supermarket.client.view.refundmodify;

import java.util.Map;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.mvpframe.client.utils.MathUtil;
import com.superman.supermarket.client.constant.OrderStatusConstants;
import com.superman.supermarket.client.constant.RefundTypeConstants;
import com.superman.supermarket.client.view.refundapplication.RefundApplicationViewImpl;

/**
 * 退款申请修改
 * 
 * @author superman
 * @version 2018年8月21日上午12:14:20
 */
public class RefundModifyViewImpl extends RefundApplicationViewImpl implements RefundModifyView {

	private double refundFee, refundableFee;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月21日 上午12:20:18
	 */
	public RefundModifyViewImpl() {
		skupanel.removeFromParent();
		pageheader.setText("申请修改");
		submitbtn.setText("确认修改");
	}

	@Override
	public void init() {
		submitbtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {

				if (priceinput.valid()) {
					double refundFee = Double.parseDouble(priceinput.getValue());
					if (refundFee == 0) {
						XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("退款金额必须大于0"));
					} else if (refundFee > refundableFee) {
						XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("退款金额超出最大可退款金额"));
						priceinput.setValue(refundableFee + "");
					} else {
						presenter.submitApplication(getApplicationData());
					}
				}

			}
		});
	}

	@Override
	public void setDefaultRefundDetail(Map<String, Object> map) {
		int orderState = (int) Double.parseDouble(String.valueOf(map.get("ORDER_STATE")));
		typelist.addItem("仅退款", RefundTypeConstants.REFUND_ONLY_MONEY + "");
		if (orderState != OrderStatusConstants.TO_DELIVERY.getStatus()) {
			typelist.addItem("退货退款", RefundTypeConstants.REFUND_MONEY_AND_RETUEN + "");
		}

		int refundType = (int) (Double.parseDouble(String.valueOf(map.get("REFUND_TYPE"))));
		typelist.setValue(refundType + "");

		String reason = String.valueOf(map.get("REASON"));
		reasonlist.setByText(reason);

		if (map.get("MEMO") != null)
			memoinput.setValue(String.valueOf(map.get("MEMO")));

		refundFee = Double.parseDouble(String.valueOf(map.get("REFUND_FEE")));
		refundableFee = Double.parseDouble(String.valueOf(map.get("REFUNDABLE_FEE")));
		refundableFee = Double.parseDouble(MathUtil.add(refundableFee + "", refundFee + ""));
		priceinput.setValue(refundFee + "");
		pricedeschtml.setHTML("最多:¥&nbsp;" + refundableFee);
	}

}
